<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Mapping the HTMLTidy library into the Application - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/app-map-filelib.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-go.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Mapping the HTMLTidy library into the Application</h1></div></div></div>
  <p>Once again, we create a custom STL exception type to represent failure
from the HTMLTidy library. We also create an <code>app</code> namespace wrapper
for the C <code>tidy_html()</code> function which is more C++ friendly.</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">app</span>
<span class="p">{</span>
  <span class="c1">// Specialise an exception type for tidylib errors
</span><span class="c1"></span>  <span class="k">struct</span> <span class="nl">tidylib_error</span> <span class="p">:</span> <span class="n">std</span><span class="o">::</span><span class="n">system_error</span>
  <span class="p">{</span>
    <span class="c1">// passthrough
</span><span class="c1"></span>    <span class="k">using</span> <span class="n">std</span><span class="o">::</span><span class="n">system_error</span><span class="o">::</span><span class="n">system_error</span><span class="p">;</span>
    <span class="n">tidylib_error</span><span class="p">()</span> <span class="o">=</span> <span class="k">default</span><span class="p">;</span>
    <span class="k">explicit</span> <span class="nf">tidylib_error</span><span class="p">(</span><span class="kt">int</span> <span class="n">c</span><span class="p">)</span>
        <span class="o">:</span> <span class="n">std</span><span class="o">::</span><span class="n">system_error</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">generic_category</span><span class="p">())</span>
    <span class="p">{</span>
    <span class="p">}</span>
  <span class="p">};</span>

  <span class="c1">// Create a C++ invoking wrapper for the tidylib C API, modifying data with the returned data,
</span><span class="c1"></span>  <span class="c1">// returing a unique_ptr to release storage on scope exit.
</span><span class="c1"></span>  <span class="k">struct</span> <span class="n">call_free</span>
  <span class="p">{</span>
    <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">T</span><span class="o">&gt;</span> <span class="kt">void</span> <span class="k">operator</span><span class="p">()(</span><span class="n">T</span> <span class="o">*</span><span class="n">p</span><span class="p">)</span> <span class="p">{</span> <span class="o">::</span><span class="n">free</span><span class="p">(</span><span class="n">p</span><span class="p">);</span> <span class="p">}</span>
  <span class="p">};</span>
  <span class="kr">inline</span> <span class="n">outcome</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="kt">char</span><span class="p">,</span> <span class="n">call_free</span><span class="o">&gt;&gt;</span> <span class="n">tidy_html</span><span class="p">(</span><span class="n">string_view</span> <span class="o">&amp;</span><span class="n">data</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="kt">char</span> <span class="o">*</span><span class="n">out</span> <span class="o">=</span> <span class="k">nullptr</span><span class="p">;</span>
    <span class="n">size_t</span> <span class="n">outlen</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">errcode</span> <span class="o">=</span> <span class="o">::</span><span class="n">tidy_html</span><span class="p">(</span><span class="o">&amp;</span><span class="n">out</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">outlen</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="n">data</span><span class="p">(),</span> <span class="n">data</span><span class="p">.</span><span class="n">size</span><span class="p">());</span>
    <span class="k">if</span><span class="p">(</span><span class="n">errcode</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="c1">// If the error code matches a standard STL exception, throw as that.
</span><span class="c1"></span>      <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">try_throw_std_exception_from_error</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">error_code</span><span class="p">(</span><span class="n">errcode</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">generic_category</span><span class="p">()));</span>
      <span class="c1">// Otherwise wrap the error code into a tidylib_error exception throw
</span><span class="c1"></span>      <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">make_exception_ptr</span><span class="p">(</span><span class="n">tidylib_error</span><span class="p">(</span><span class="n">errcode</span><span class="p">));</span>
    <span class="p">}</span>
    <span class="c1">// Reset input view to tidied html
</span><span class="c1"></span>    <span class="n">data</span> <span class="o">=</span> <span class="n">string_view</span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">outlen</span><span class="p">);</span>
    <span class="c1">// Return a unique ptr to release storage on scope exit
</span><span class="c1"></span>    <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="kt">char</span><span class="p">,</span> <span class="n">call_free</span><span class="o">&gt;</span><span class="p">(</span><span class="n">out</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>  <span class="c1">// namespace app
</span><span class="c1"></span></code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/finale.cpp#L336" class="code-snippet-url" target="_blank">View this code on Github</a></div>



        </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/app-map-filelib.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-go.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
